//
//  settleDeliveryCell.m
//  ProjectforHDX
//
//  Created by motom on 16/8/8.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "settleDeliveryCell.h"
#import "SettleDeliverPlate.h"
@interface settleDeliveryCell()
{
    SettleDeliverPlate  *_plate;
}


//!板件名
@property (nonatomic, strong) RZCustomLable *plateName;
@property(nonatomic,strong)RZCustomLable *plateNameText;
//!板件号
@property (nonatomic, strong) UILabel *plateNum;
@property(nonatomic,strong)RZCustomLable *plateNumText;

//!版本号
@property (nonatomic, strong) RZCustomLable *plateVersionNum;
@property(nonatomic,strong)RZCustomLable *plateVersionNumText;

//!货权
@property (nonatomic, strong) RZCustomLable *plateCompany;
@property(nonatomic,strong)RZCustomLable *plateCompanyText;

//!序列号
@property (nonatomic, strong) RZCustomLable *plateSerialNum;
@property(nonatomic,strong)RZCustomLable *plateSerialNumText;


//!数量
@property (nonatomic, strong) RZCustomLable *plateNumber;
@property (nonatomic, strong) RZCustomLable *plateNumberText;



@end

@implementation settleDeliveryCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupView];
    }
    return self;
}
#define CELL_HEIGHT 70
//初始化控件
-(void)setupView
{
    self.editing = NO;
    self.containView = [[UIView alloc]init];
    self.containView.clipsToBounds = YES;
    
    [self addSubview:self.containView];
    
    
    self.plateName = [[RZCustomLable alloc]init];
    [self.plateName setFont:CustomFont(13)];
    self.plateName.text = [NSString stringWithFormat:@"%@: ",[UtilCheck language:@"setting.plateName"]];
    [self.plateName setTextColor:[UtilCheck hexStringToColor:@"858E99" alpha:1]];
    [self.containView addSubview:self.plateName];
    
    self.plateNum = [[UILabel alloc]init];
    [self.plateNum setFont:CustomFont(13)];
    self.plateNum.text  = [NSString stringWithFormat:@"%@: ",[UtilCheck language:@"setting.plateNO"]];
    [self.plateNum setTextColor:[UtilCheck hexStringToColor:@"858E99" alpha:1]];
    [self.containView addSubview:self.plateNum];
    
    self.plateSerialNum = [[RZCustomLable alloc]init];
    [self.plateSerialNum setFont:CustomFont(13)];
    self.plateSerialNum.text  = [NSString stringWithFormat:@"%@: ",[UtilCheck language:@"setting.plateSerial"]];
    [self.plateSerialNum setTextColor:[UtilCheck hexStringToColor:@"858E99" alpha:1]];
    [self.containView addSubview:self.plateSerialNum];
    
    self.plateVersionNum = [[RZCustomLable alloc]init];
    [self.plateVersionNum setFont:CustomFont(13)];
    self.plateVersionNum.text  = [NSString stringWithFormat:@"%@: ",[UtilCheck language:@"setting.plateCode"]];
    [self.plateVersionNum setTextColor:[UtilCheck hexStringToColor:@"858E99" alpha:1]];
    [self.containView addSubview:self.plateVersionNum];
    
    self.plateCompany = [[RZCustomLable alloc]init];
    [self.plateCompany setFont:CustomFont(13)];
    self.plateCompany.text  = [NSString stringWithFormat:@"%@: ",[UtilCheck language:@"seeting.goodWeight"]];
    [self.plateCompany setTextColor:[UtilCheck hexStringToColor:@"858E99" alpha:1]];
    [self.containView addSubview:self.plateCompany];
    
    self.alterBtn = [[RZCustomButtom alloc]init];
    self.alterBtn.titleLabel.font= CustomFont(14);
    [self.alterBtn setTitle:[UtilCheck language:@"Delivery.Manual"] forState:UIControlStateNormal];
    [self.alterBtn setTitleColor:[UtilCheck hexStringToColor:@"1A57BE" alpha:1.0] forState:UIControlStateNormal];
    [self.alterBtn setImage:BDFILEIMAGENAEM(@"Order/btn_update")forState:UIControlStateNormal];
    [self.containView addSubview:self.alterBtn];
    self.alterBtn.right = SCREEN_WIDTH - 80;
    self.alterBtn.top = 15;
    [self.alterBtn sizeToFit];
    
    self.plateNumber = [[RZCustomLable alloc]init];
    self.plateNumber.font = CustomFont(14);
    self.plateNumber.text = [NSString stringWithFormat:@"%@:",[UtilCheck language:@"order.count"]];
    self.plateNumber.textColor = [UtilCheck hexStringToColor:@"858E99" alpha:1];
    [self.plateNumber sizeToFit];
    [self.containView addSubview:self.plateNumber];
    
    
    
    [self.alterBtn addTarget:self action:@selector(changeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
   
}

-(void)changeBtnClick:(UIButton *)btn
{
    self.clickBlock(btn.tag);
}


#define lableHDistance 80
#define lableVDistance 3
-(void)layoutSubviews
{
    if (_plate) {
        
        self.plateName.top = 6;
        self.plateName.left = 10;
        [self.plateName sizeToFit];
        
        self.plateNum.top = self.plateName.top;
        if ([[THMangerTool language:@"CSSettleDeliveryUpdatePlateTVC.title"] isEqualToString:@"Delivery"]) {
            self.plateNum.left = self.plateName.right + lableHDistance;
        }else {
            self.plateNum.left = self.plateName.right + 50;
        }
        [self.plateNum sizeToFit];
        
        
        CGFloat marW = self.plateNum.left - self.plateName.right;
        CGFloat marW2 = self.alterBtn.left - self.plateNum.right+20;
        
        //板件名
        CGSize size = [self.plateNameText.text boundingRectWithSize:CGSizeMake(marW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size;
        [self.plateNameText setFrame:CGRectMake(self.plateName.right, self.plateName.top, size.width, self.plateNum.height)];
        
        //板件号
        CGSize size2 = [self.plateNumText.text boundingRectWithSize:CGSizeMake(marW2, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size;
        
        [self.plateNumText setFrame:CGRectMake(self.plateNum.right, self.plateName.top, size2.width, self.plateNum.height)];
        
        CGFloat height = 0;
        if (size.height > size2.height) {
            height = self.plateNameText.bottom;
        }
        else{
            height = self.plateNumText.bottom;
        }
        
        self.plateVersionNum.top = height + lableVDistance;
        self.plateVersionNum.left = self.plateName.left;
        [self.plateVersionNum sizeToFit];
        
        self.plateSerialNum.top = self.plateVersionNum.top;
        self.plateSerialNum.left = self.plateNum.left;
        [self.plateSerialNum sizeToFit];
        
        self.plateNumber.left = self.plateSerialNum.left;
        self.plateNumber.top = self.plateCompany.top;
        
//        //版本号
        CGSize size3 = [self.plateVersionNumText.text boundingRectWithSize:CGSizeMake(marW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size;
        [self.plateVersionNumText setFrame:CGRectMake(self.plateVersionNum.right, self.plateVersionNum.top, size3.width, self.plateVersionNum.height)];

        //序列号
        CGSize size4 = [self.plateSerialNumText.text boundingRectWithSize:CGSizeMake(marW2, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size;
        [self.plateSerialNumText setFrame:CGRectMake(self.plateSerialNum.right, self.plateVersionNum.top, size4.width, self.plateSerialNum.height)];

        height = self.plateVersionNumText.bottom;

        self.plateCompany.top = height + lableVDistance;
        self.plateCompany.left = self.plateName.left;
        [self.plateCompany sizeToFit];

        //货权
        CGSize size5 = [self.plateCompanyText.text boundingRectWithSize:CGSizeMake(marW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size;
        
        CGFloat plateCompanyRight = self.plateCompany.right + size5.width;
        
        
        if (plateCompanyRight < self.plateNum.left) { // 如果太长就换行
            
           [self.plateCompanyText setFrame:CGRectMake(self.plateCompany.right, self.plateCompany.center.y - size5.height * 0.5, size5.width, self.plateCompany.height)];
        }
        else {
            [self.plateCompanyText setFrame:CGRectMake(self.plateCompany.left, self.plateCompany.bottom, size5.width, self.plateCompany.height)];
        }
        
        //数量
        [self.plateNumberText sizeToFit];
        self.plateNumberText.left = self.plateNumber.right;
        self.plateNumberText.top = self.plateNumber.top;
        
        self.cellHeight = self.plateCompanyText.bottom +10;
     }
    else
    {
    self.plateName.top = 6;
    self.plateName.left = 10;
    [self.plateName sizeToFit];
    
    self.plateNum.top = self.plateName.top;
    if ([[THMangerTool language:@"CSSettleDeliveryUpdatePlateTVC.title"]isEqualToString:@"Delivery"]) {
        self.plateNum.left = self.plateName.right + lableHDistance;
    }else {
        self.plateNum.left = self.plateName.right + 50;
    }
    [self.plateNum sizeToFit];
    
    self.plateVersionNum.top = self.plateName.bottom + lableVDistance;
    self.plateVersionNum.left = self.plateName.left;
    [self.plateVersionNum sizeToFit];
    
    self.plateSerialNum.top = self.plateVersionNum.top;
    self.plateSerialNum.left = self.plateNum.left;
    [self.plateSerialNum sizeToFit];
    
    self.plateCompany.top = self.plateVersionNum.bottom +lableVDistance;
    self.plateCompany.left = self.plateName.left;
    [self.plateCompany sizeToFit];
        
    self.plateNumber.left = self.plateSerialNum.left;
    
    }
    [self.containView addSoldLine:1.0 soldcolor:[UtilCheck hexStringToColor:@"e1e1df" alpha:1.0] pointA:CGPointMake(0, self.cellHeight) pointB:CGPointMake(self.containView.width, self.cellHeight)];

}

-(void)setPlateModel:(SettleDeliverPlate *)plate
{
//    self.plateName.attributedText =[self setPlateMessage:self.plateName.text plateStr:plate.name];
    if (_plate) {
        _plate = plate;
        self.plateNameText.text = plate.partsName;
        
        self.plateNumText.text = plate.partsCode;
        
        self.plateVersionNumText.text = plate.partsVersion;
        
        self.plateSerialNumText.text = plate.partsSN;
        
        self.plateCompanyText.text = plate.attribution;
        
        self.plateNumberText.text = plate.partsCount;
       
        [self layoutSubviews];
        [self.containView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.cellHeight)];
    
        return;
    }
    _plate = plate;
    
    //板件名
    RZCustomLable *plateName = [[RZCustomLable alloc]init];
    plateName.numberOfLines = 1;
    plateName.text = plate.partsName;
    self.plateNameText = plateName;
    self.plateNameText.lineBreakMode = NSLineBreakByTruncatingMiddle;
//    self.plateNameText.adjustsFontSizeToFitWidth = NO;
    [self.plateNameText setTextColor:[UtilCheck hexStringToColor:@"000000" alpha:1.0]];
    self.plateNameText.font = CustomFont(13);
    [self addSubview:plateName];
    
    //板件号
    RZCustomLable *plateNo = [[RZCustomLable alloc]init];
    plateNo.numberOfLines  = 1;
    plateNo.text = plate.partsCode;
    self.plateNumText = plateNo;
//    self.plateNumText.lineBreakMode = NSLineBreakByTruncatingMiddle;
//    self.plateNumText.adjustsFontSizeToFitWidth = NO;
    [self.plateNumText setTextColor:[UtilCheck hexStringToColor:@"000000" alpha:1.0]];
    self.plateNumText.font =CustomFont(13);
    [self addSubview:plateNo];
    
    //版本号
    RZCustomLable *plateVersion = [[RZCustomLable alloc]init];
    plateVersion.numberOfLines = 1;
    plateVersion.text = plate.partsVersion;
    self.plateVersionNumText = plateVersion;
    self.plateVersionNumText.lineBreakMode = NSLineBreakByTruncatingMiddle;
//    self.plateVersionNumText.adjustsFontSizeToFitWidth = NO;
    self.plateVersionNumText.font = CustomFont(13);
    [self.plateVersionNumText setTextColor:[UtilCheck hexStringToColor:@"000000" alpha:1.0]];

    [self addSubview:plateVersion];
    
    //序列号
    RZCustomLable *plateSerialNum = [[RZCustomLable alloc] init];
    plateSerialNum.numberOfLines = 1;
    plateSerialNum.text = plate.partsSN;
    self.plateSerialNumText = plateSerialNum;
    self.plateSerialNumText.lineBreakMode = NSLineBreakByTruncatingMiddle;
//    self.plateSerialNumText.adjustsFontSizeToFitWidth = NO;
    self.plateSerialNumText.font = CustomFont(13);
    [self.plateSerialNumText setTextColor:[UtilCheck hexStringToColor:@"000000" alpha:1.0]];

    [self addSubview:plateSerialNum];
    
    //货权
    RZCustomLable *plateCompany = [[RZCustomLable alloc] init];
    plateCompany.numberOfLines = 1;
    plateCompany.text = plate.attribution;
    self.plateCompanyText = plateCompany;
    self.plateCompanyText.lineBreakMode = NSLineBreakByTruncatingMiddle;
//    self.plateCompanyText.adjustsFontSizeToFitWidth = NO;
    self.plateCompanyText.font = CustomFont(13);
    [self.plateCompanyText setTextColor:[UtilCheck hexStringToColor:@"000000" alpha:1.0]];
    [self addSubview:plateCompany];
    
    
    //数量
    RZCustomLable *plateCount = [[RZCustomLable alloc] init];
    plateCount.numberOfLines = 1;
    plateCount.text = plate.partsCount;
    self.plateNumberText = plateCount;
    self.plateNumberText.lineBreakMode = NSLineBreakByTruncatingMiddle;
//    self.plateNumberText.adjustsFontSizeToFitWidth = NO;
    self.plateNumberText.font = CustomFont(13);
    [self.plateNumberText setTextColor:[UtilCheck hexStringToColor:@"000000" alpha:1.0]];
    [self addSubview:plateCount];

    
    [self layoutSubviews];
    [self.containView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.cellHeight)];
    
     //
    
    
}

-(NSAttributedString *)setPlateMessage:(NSString *)labelName plateStr:(NSString *)plateName
{
    NSMutableAttributedString *labelStr = nil ;
    NSAttributedString *plateStr= nil;
    labelStr = [[NSMutableAttributedString alloc]initWithString:labelName];
    plateStr = [[NSAttributedString alloc]initWithString:plateName attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
    [labelStr appendAttributedString:plateStr];
    return labelStr;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
